const fetch = require("node-fetch");
const cheerio = require("cheerio");

/**
 * 测试
 */
const default_config = {
    name: "test"
}
const utils = {}
class Spider {
    /**
     * 初始化
     * @param {*} config 
     */
    constructor(config) {
        config = Object.assign({}, default_config, config);
        this.timer = null;
        this.tasks = [];
    }
    /**
     * 添加任务
     * @param {*} obj 
     * @param {*} config 
     */
    add(obj, config) {
        this.tasks.push(new obj(config));
        return this;
    }
    /**
     * 开始执行
     */
    start() {
        this.timer = setInterval(() => this._run(), 1000);
    }
    _run() {
        this.tasks.forEach(item => {
            if (!item.running) item._start();
        })
    }
}

class Task {
    constructor() {
        this.running = false;
    }
    /**
     * 启动任务
     */
    _start() {
        this.running = true;
        setTimeout(() => {
            this.running = false;
        }, 2000)
    }
}
module.exports = {
    Spider,
    Task
}